<?php

class BlogController extends Site_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        Zend_Loader::loadClass('datetools','library');
        
        $sessao = new Zend_Session_Namespace('Parametros');
        $this->view->parametros  = $sessao->parametros;
        
        $where = 'BLO_ID > 0 and ';
        
        // item
        $id = $this->_getParam('id', 0);
        $this->view->id = $id;
                
        // categoria
        $cat = $this->_getParam('cat', 0);
        if($cat > 0) $where = 'CTB_ID = '.$cat.' AND ';
        
        // tag
        $tag = $this->_getParam('tag', 0);
        if($tag > '') $where = 'BLO_TAG like "'.$tag.'" AND ';
        
        // tag
        $ano = $this->_getParam('ano', 0);
        $mes = $this->_getParam('mes', 0);
        if($ano > 0 && $mes > 0) $where = 'YEAR(BLO_DAT) like "'.$ano.'" AND MONTH(BLO_DAT) like "'.$mes.'" AND ';
        
        $query = 'SELECT SQL_CACHE * FROM BLOG WHERE '.$where.' BLO_STA = 1 ORDER BY BLO_DAT DESC';
        $obj = $this->db->fetchAll($query);
        $this->view->blog = $this->paginacao($obj,10);
        
        
        $query = 'SELECT SQL_CACHE DISTINCT BLO_TAG FROM BLOG WHERE BLO_STA = 1 ORDER BY BLO_TAG ASC';
        $obj = $this->db->fetchAll($query);
        $html = '';
        foreach($obj as $tag){
            $html .= '<a href="'. $this->_urlbase . '/blog/index/tag/'.$tag['BLO_TAG'].'">'.$tag['BLO_TAG'].'</a>, ';
        }
        if($html > '') $html = substr($html,0,-2);
        $this->view->tag = $html;    
        
        
        $query = 'SELECT SQL_CACHE MONTH(BLO_DAT) as mes, YEAR(BLO_DAT) as ano FROM BLOG
                    WHERE BLO_STA = 1
                    GROUP BY YEAR(BLO_DAT),MONTH(BLO_DAT) 
                    ORDER BY YEAR(BLO_DAT) DESC,MONTH(BLO_DAT) DESC';
        $obj = $this->db->fetchAll($query);
        $this->view->meses = $obj;
            
    }
    
    
    
   

}

